package utils;

import entry.Goods;
import entry.Library;
import entry.Location;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class InputUtil {

    private  DataInputStream in;
    private  BufferedReader br;

    //根据文件名字，初始化类路径下的该文件的输入流
    public InputUtil(String fileName){
        String path = this.getClass().getClassLoader().getResource(fileName).getPath();
        try {
            in = new DataInputStream(new FileInputStream(new File(path)));
            //br = new BufferedReader(new InputStreamReader(in, "GBK"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public List<Goods> getAllGoods() {
        List<Goods> goods = new ArrayList<Goods>();
        String line = null;
        try {
            br = new BufferedReader(new InputStreamReader(in, "GBK"));
            line = br.readLine();
            while ((line = br.readLine()) != null) {
                String item[] = line.split("\t");
                goods.add(new Goods(item[0], item[1]));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goods;
    }

    public List<List<Goods>> classify(List<Goods> goods){
        List<List<Goods>> goodsClassify = new ArrayList<List<Goods>>();
        List<Goods> goodsList = new ArrayList<Goods>();
        String lastType = goods.get(0).getType().trim();
        for(Goods goods1:goods){
            if(!(goods1.getType().equals(lastType))){
                goodsClassify.add(goodsList);
                lastType = goods1.getType();
                goodsList=new ArrayList<Goods>();
            }
            goodsList.add(goods1);
        }
        goodsClassify.add(goodsList);
        return goodsClassify;
    }

    public static int getTypeNum(List<Goods> goodsList){
        Library library = new Library();
        for (Goods goods:goodsList){
            library.put(goods.getType());
        }
        System.out.println(library);
        return library.size();
    }

    public Library getLibrary(){
        Library library = new Library();
        String line = null;
        try {
            br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            while ((line = br.readLine()) != null){
                library.put(line.substring(line.indexOf("-->")+3));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return library;
    }

    public List<Location> getLocations() {
        List<Location> locations = new ArrayList<Location>();
        String line = null;
        try {
            br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            while ((line = br.readLine()) != null){
                Location location = new Location();
                location.setName(line.substring(line.indexOf("-->")+3,line.indexOf("{")-3));
                String lication[] = line.substring(line.indexOf("{")+1,line.indexOf("}")).split(",");
                for(String str : lication){
                    location.put(Integer.parseInt(str.substring(0,str.indexOf("=")).trim()),
                            Double.parseDouble(str.substring(str.indexOf("=")+1).trim()));
                }
                locations.add(location);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return locations;
    }
}
